#include "stdio.h"
#include <fcntl.h>
#include <sys/ioctl.h>
#include "private.h"
#include "e22Control.h"
#include "crc.h"


typedef signed int __int32_t;
typedef unsigned int __uint32_t;

#define UART_IOC_MAGIC   'u'
#define UART_CFG_BAUDRATE _IO(UART_IOC_MAGIC, 1)
#define UART_CFG_ATTR     _IOW(UART_IOC_MAGIC, 5, __int32_t)

E22Control* e22ControlInit()
{
    int fd = -1;
    int ret = 0;
    E22Control *e22Ctrl = NULL;
    e22Ctrl = (E22Control*)malloc(sizeof(E22Control));
    if (e22Ctrl == NULL){
        printf("error:  malloc motorControl CB error \n");
        return NULL;
    }
    fd=open(E22_DEV, O_RDWR);
    if(fd == -1){
        printf("error:open %s error\n", E22_DEV);
        return -1;
    }
    ioctl(fd,UART_CFG_BAUDRATE,9600);//设置默认通信波特率
    e22Ctrl->fd = fd;

    return e22Ctrl;
}

int e22ControlRead(E22Control* e22Ctrl, unsigned char * buf, int bufSize)
{
    int ret = 0;
    ret = read(e22Ctrl->fd, buf, bufSize);

    return ret;
}

int e22ControlWrite(E22Control* e22Ctrl, unsigned char *buf, int bufSize)
{
    int ret = 0;
    ret = write(e22Ctrl->fd, buf, bufSize);
    return ret;
}

int e22ControlRelease(E22Control* e22Ctrl)
{
    close(e22Ctrl->fd);
    free(e22Ctrl);
    return 0;
}
